#include<stdio.h>
#include<stdlib.h>

int len;
void inspos(int *,int,int);
void display(int *);
void append(int *,int);
int count(int *,int);
int index(int *,int);
void reverse(int *);
void pop(int *,int);
void remuv(int *,int);
void extend(int *);
void sorts(int *);
void main()
{
printf("Enter size of list : ");
scanf("%d",&len);
int *p,i,m;
p=(int*)malloc(sizeof(int) * len);  
printf("Enter %d list elements \n",len);
for(i=0;i<len;i++)
{
printf("Enter %d element ",(i+1));
scanf("%d",&p[i]);}
display(p);
while(m!=0)
{
printf("ENTER FUNCTION NO.\n 1. Insert \n 2. Append \n 3. Length \n 4. Count \n 5. Index \n 6. Reverse \n 7. Pop \n 8. Remove \n 9. Extend \n 10. Sort \n Press 0 for exit \n");
scanf("%d",&m);

switch(m)
{
case 1:
{
int num1,pos;
printf("Enter position no. (starts from 1st position or you can also enter negative index)");
scanf("%d",&pos);
printf("Enter element to be insert "); 
scanf("%d",&num1);
if(pos<0)
pos=len+1+pos;
inspos(p,num1,pos);
display(p);
break;
}
case 2:
{
int num2;
printf("Enter element to be append "); 
scanf("%d",&num2);
append(p,num2);
display(p);
break;
}
case 3:
{
printf("Length of list is %d \n",len);
break;
}
case 4:
{
int l;
printf("Enter no ");
scanf("%d",&l);
printf("\n No. of given no are %d \n",(count(p,l)));
break;
}
case 5:
{
int l;
printf("Enter no ");
scanf("%d",&l);
printf("\n Index of given no. %d \n",(index(p,l)));
break;
}
case 6:
{
reverse(p);
display(p);
break;
}
case 7:
{int k;
printf("Enter position to be pop (starts from 1st postion or Enter negative index) \n" );
scanf("%d",&k);
if(k<0)
k=len+1+k;
pop(p,k);
display(p);
break;
}
case 8:
{
int k;
printf("Enter element to delete \n");
scanf("%d",&k);
remuv(p,k);
display(p);
break;
}
case 9:
{
extend(p);
display(p);
break;
}
case 10:
{
sorts(p);
display(p);
break;
}

}
}
}

void inspos(int *ptr,int num2,int pos)
{
int i;
if (pos<=0 || pos>len+1)
{
printf("invalid position");
}
else
{
ptr=(int*)realloc(ptr, (sizeof(int) * (len+1)));
for(i=len;i>=pos-1;i--)
{
ptr[i+1]=ptr[i];}
ptr[pos-1]=num2;
len++;
}
}

void display(int *ptr1)
{ printf("[");
int i;
for(i=0;i<len;i++)
{
printf(" %d ",ptr1[i]);
if((i+1)!=len)
printf(",");
}
printf("] \n");
}

void append(int *ptr,int num)
{
ptr=(int*)realloc(ptr, (sizeof(int) * (len+1)));
ptr[len]=num;
len++;
}

int count(int *ptr,int num)
{
int i,c;
c=0;
for(i=0;i<len;i++)
{
if(ptr[i]==num)
c++;
}
return c;
}

int index(int *ptr,int num)
{
int i,pos=-1;
for(i=0;i<len;i++)
{
if(ptr[i]==num)
{pos=i;
break;
}
}
return pos;
}

void reverse(int *ptr)
{
int i,tmp;
for(i=0;i<(len/2);i++)
{
tmp=ptr[len-1-i];
ptr[len-1-i]=ptr[i];
ptr[i]=tmp;
}
}

void pop(int *ptr,int pos)
{
int i;
pos=pos-1;
if (pos<=0 || pos>=len)
{
printf("Invalid position \n");
return;
}
else
{
printf("\n Element poped out is %d \n",ptr[pos]);
for(i=pos;i<len;i++)
{
ptr[i]=ptr[i+1];
}
ptr=(int*)realloc(ptr, (sizeof(int) * (len-1)));

len--;
}
}

void remuv(int *ptr,int num)
{
int i,j,pos;
for(j=0;j<len;j++)
{
if(ptr[j]==num)
{pos=j;
break;
}
}

for(i=pos;i<len;i++)
{
ptr[i]=ptr[i+1];
}
ptr=(int*)realloc(ptr, (sizeof(int) * (len-1)));
len--;
}

void extend(int *ptr)
{
int i,k;
printf("Enter the number of elements in list to be extend");
scanf("%d",&k);
ptr=(int*)realloc(ptr, (sizeof(int) * (len+k)));
printf("\nEnter %d list elements \n",k);
for(i=0;i<k;i++)
{
printf("Enter %d element ",(i+1));
scanf("%d",&ptr[len+i]);
}
len=len+k;  
}  

void sorts(int *ptr)
{
int i,j,tmp;
for(i=0;i<len;i++)
{
for(j=0;j<len-i-1;j++)
{
if(ptr[j]>ptr[j+1])
{
tmp=ptr[j+1];
ptr[j+1]=ptr[j];
ptr[j]=tmp;
}
}
}
}   
